package com.mall.b2c.template.directive;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.mall.b2c.bean.BrandBean;
import com.mall.b2c.hessian.GoodsExtendHessian;
import com.mall.common.directive.DirectiveUtils;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 品牌列表标签
 *
 */
@Repository
public class BrandListDirective extends BaseDirective {
	
	public static final String IS_RECOMMEND = "isRecommend";
	public static final String COUNT = "count";
	public static final String GOODS_CATEGORY_CODE = "gcCode";
	
	@Resource private GoodsExtendHessian goodsExtendHessian;
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		Boolean isRecommend = DirectiveUtils.getBool(IS_RECOMMEND, params);
		Integer count = DirectiveUtils.getInt(COUNT, params);
		String goodsCategoryCode = DirectiveUtils.getString(GOODS_CATEGORY_CODE, params); 
		
		String orderBy = DirectiveUtils.getString("orderBy", params); 
		List<BrandBean> list = 
				jsonToList(goodsExtendHessian.getBrandList(null, isRecommend, count, goodsCategoryCode, orderBy),
				BrandBean.class);
		renderParamToBody("brandList", list, env, body);
	}
	
}
